package cn.zmit.kuxi.activity;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.request.RequestTask;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.common.utils.StringUtils;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

/** * @author @parameter * @return */
public class RegisteredTwoActivity extends BaseActivity {
	@ViewInject(R.id.tv_regsit_login)
	private TextView tv_regsit_login;
	@ViewInject(R.id.edt_iput_username)
	private EditText edt_iput_username;
	@ViewInject(R.id.edt_set_pwd)
	private EditText edt_set_pwd;
	@ViewInject(R.id.edt_set_secound_pwd)
	private EditText edt_set_secound_pwd;
	@ViewInject(R.id.cbox_accept_kuxi)
	private CheckBox cbox_accept_kuxi;
	// @ViewInject(R.id.tv_user_agreement)
	// private TextView tv_user_agreement;
	@ViewInject(R.id.btn_submit)
	private Button btn_submit;
	@ViewInject(R.id.llRoot)
	private LinearLayout llRoot;
	private String phone;
	private String verification_code;
	String iput_username;
	String set_pwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registered_two);
		ViewUtils.inject(this);
		// 接收从上个界面传来的值
		phone = getTextFromBundle("phone");
		llRoot.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

				return inputMethodManager.hideSoftInputFromWindow(
						getCurrentFocus().getWindowToken(), 0);
			}
		});
	}

	@OnClick({ R.id.btn_submit, R.id.tv_regsit_login })
	private void OnClick(View v) {
		switch (v.getId()) {
		case R.id.btn_submit:
			if (checkForm()) {
				// 此处需要判断checkbox是否选中选中才可以点击到提交
				if (cbox_accept_kuxi.isChecked()) {
					// iput_username =
					// edt_iput_username.getText().toString().trim();
					set_pwd = edt_set_pwd.getText().toString().trim();
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("phone", phone);
					map.put("name", "");
					map.put("password", set_pwd);
					map.put("from", "米赚");
					RequestTask.getInstance().doRegist(this, map,
							new OnRequestListener());
				} else {
					Toast.makeText(context, "需要同意相关协议！", 0).show();
				}

			}

			break;
		case R.id.tv_regsit_login:
			openActivity(LoginActivity.class);
			break;

		default:
			break;
		}
	}

	// 请求
	private class OnRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);
			JsonData jsonData = JsonData.create(response);
			String data = jsonData.optString("code");
			if (data.equals("0") && data != null) {
				ToastUtils.show(context, jsonData.optString("message"));
				String userIdString = jsonData.optString("user_id");
				PreferenceHelper.write(context,
						SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID,
						userIdString);
				openActivity(HomeActivity.class);

			} else {

				// ToastUtils.show(context, jsonData.optString("message"));
				ToastUtils.show(context, "注册失败");
			}
			// 设置已登陆
			PreferenceHelper.write(context, SPConstants.SHARED_PREFERENCE_NAME,
					SPConstants.HAS_LOGGED, true);

		}
	}

	private boolean checkForm() {
		// iput_username = edt_iput_username.getText().toString().trim();
		set_pwd = edt_set_pwd.getText().toString().trim();
		String secound = edt_set_secound_pwd.getText().toString().trim();
		// if (StringUtils.isEmpty(iput_username)) {
		// ToastUtils.show(this, "昵称不能为空");
		// return false;
		// }else

		if (StringUtils.isEmpty(set_pwd)) {
			ToastUtils.show(this, "密码不能为空");
			return false;
		} else if (set_pwd.length() < 6 || set_pwd.length() > 20) {
			ToastUtils.show(this, "密码长度为6-20个字符");
			return false;
		} else if (!set_pwd.equals(secound)) {
			ToastUtils.show(this, "两次输入的密码不相同");
			return false;
		}
		return true;
	}
}
